package com.szholly.plug.safe.entity.role;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;
import com.szholly.plug.safe.entity.user.UserRoleEntity;
import com.szholly.plug.safe.entity.user.UserRoleService;

/**
 * 角色表
 */
@Component("SAFE_ROLE")
public class RoleService extends InterceptorTableBase implements IBaseTable {
	public final static String TABLE_NAME = "SAFE_ROLE";

	/**
	 * 获取单例对象
	 */
	public static RoleService getSingleRef() {
		return (RoleService) SpringBeanFactory.getBean("SAFE_ROLE");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "角色表";
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();

			itemField = new Field();
			itemField.setFieldName(RoleEntity.FIELD_I_ROLEID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("主键");
			itemField.setIsKey(true);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RoleEntity.FIELD_C_ROLENAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("角色名");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RoleEntity.FIELD_C_ROLEDESC);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("角色描述");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName("S_COMPANY");
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("所属机构");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName("S_COMPANYNAME");
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("所属机构");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
		}
		return fields;
	}
	
	private RelationNode relationNode;
	/**
	 * 表关系
	 */
	@Override
	public RelationNode getRelation() {
		if (relationNode == null) {
			relationNode = new RelationNode(TABLE_NAME);

			// 角色与功能权限
			RelationInfo relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(RoleEntity.FIELD_I_ROLEID, RoleFunctionEntity.FIELD_ROLEID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "角色与功能权限";
			relationNode.getChildRelationNode().put(RoleFunctionService.TABLE_NAME, relationInfo);
			
			// 角色与数据权限
			relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(RoleEntity.FIELD_I_ROLEID, RoleSQLEntity.FIELD_ROLEID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "角色与数据权限";
			relationNode.getChildRelationNode().put(RoleSQLService.TABLE_NAME, relationInfo);
			
			// 角色与用户权限
			relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(RoleEntity.FIELD_I_ROLEID, UserRoleEntity.FIELD_ROLEID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "角色与用户权限";
			relationNode.getChildRelationNode().put(UserRoleService.TABLE_NAME, relationInfo);
		}
		return relationNode;
	}

	/**
	 * 表分组信息
	 */
	@Override
	public String getTableGroup() {
		return "用户权限控制";
	}

	@Override
	public String getCodeFieldName() {
		return RoleEntity.FIELD_I_ROLEID;
	}

	@Override
	public String getCaptionFieldName() {
		return RoleEntity.FIELD_C_ROLENAME;
	}

	@Override
	public boolean getIsCodeTable() {
		return true;
	}

	@Override
	public boolean getIsTreeTable() {
		return false;
	}

	@Override
	public boolean getIsManageCodeTable() {
		return false;
	}
	
	@Override
	public String getGUIDFieldName(){
		return RoleEntity.FIELD_I_ROLEID;
	}
}